﻿using System.Linq;
using Loki.UI;
using Loki.Utils;

namespace Luna.UI.Main
{
    public class DocumentsViewModel : ContainerOneActive<Screen>, IHandle<INavigationMessage>
    {
        public DashboardViewModel Dashboard
        {
            get { return Items.OfType<DashboardViewModel>().FirstOrDefault(); }
            set { EnsureItem(value); }
        }

        protected override void OnActivate()
        {
            base.OnActivate();
            if (ActiveItem == null && Dashboard != null)
            {
                ActivateItem(Dashboard);
            }
        }

        public void Handle(INavigationMessage message)
        {
            Screen item = null;

            foreach (var candidate in Children)
            {
                if (message.Match(candidate))
                {
                    item = candidate;
                }
            }

            if (item == null)
            {
                item = Context.Get(message.NavigateTo) as Screen;
                EnsureItem(item);
                message.Initialize(item);
            }

            ActivateItem(item);
        }
    }
}